#include "MainWindow.h"

MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent){
	setupUi(this);
	fps = 60;
	//this->setCentralWidget(this->graphicsViewRight);

	this->m_producer = NULL;
	this->m_consumer = NULL;
	this->isPlaying = false;
	this->extractionManagerWindow = new ExtractionManagerWindow();
	timer = new QTimer();
	timer->start(200);
	this->centralWidget()->setLayout(verticalLayout);
	this->connectSlots();
}


MainWindow::~MainWindow(){
	delete m_consumer;
	delete m_producer;
	delete m_profile;
	Mlt::Factory::close();
}

void MainWindow::initializeMlt(){

	// Initialize the library and load plugins.
	Mlt::Factory::init (NULL);

	m_profile = new Mlt::Profile ("square_ntsc_wide");
	m_consumer = new Mlt::Consumer (*m_profile, "sdl_preview");

	// Embed the SDL window in our GUI.
	m_consumer->set("window_id", (int) this->videoWidget->winId());
	m_consumer->set("play.audio_buffer", 4096);
	QPalette pal;
	m_consumer->set("window_background", pal.color(QPalette::Window).name().toAscii().constData());

	// Load a color producer to clear the video region with black.
	m_producer = new Mlt::Producer(*m_profile, "color:");
	m_consumer->connect(*m_producer);
	m_producer->set_speed(0.0);
	m_consumer->start();
	DataManagerSingleton::getInstance()->initializeMlt(extractionManagerWindow->getVideoWidgetId());
}

void MainWindow::connectSlots(){
	connect(this->actionOuvrir, SIGNAL(triggered()), this, SLOT(openVideo()));
	connect(this->actionLire, SIGNAL(triggered()), this, SLOT(playOrPause()));
	connect(this->actionArreter, SIGNAL(triggered()), this, SLOT(arreter()));
	connect(this->positionSlider, SIGNAL(sliderMoved(int)), this, SLOT(seek(int)));
	connect(this->buttonLire, SIGNAL(clicked()), this, SLOT(playOrPause()));
	connect(this->buttonArreter, SIGNAL(clicked()), this, SLOT(arreter()));
	//connect(this->buttonVoireListe, SIGNAL(clicked()), extractionManagerWindow, SLOT(show()));
	connect(this->buttonVoireListe, SIGNAL(clicked()), this, SLOT(showExtractionManagerWindow()));
	connect(this->buttonExtraire, SIGNAL(clicked()), this, SLOT(extraire()));
	connect(timer, SIGNAL(timeout()), this, SLOT(updateSlider()));
}

void MainWindow::openVideo(){
	QString fileName = QFileDialog::getOpenFileName(this);
	if(!fileName.isNull()){
		Mlt::Producer *producer = new Mlt::Producer(*m_profile, fileName.toUtf8().constData());
		if(producer->is_valid()){
			m_consumer->stop();
			m_consumer->connect(*producer);
			delete m_producer;
			m_producer = producer;
			m_consumer->start();
			play();
		}
		DataManagerSingleton::getInstance()->openVideo(fileName);
		this->extractionManagerWindow->setSlidersMaximum(DataManagerSingleton::getInstance()->getLengthVideo());
		fps = m_producer->get_fps();
	}
	// If file invalid, then on some platforms the dialog messes up SDL.
	m_consumer->set ("refresh", 1);
}

void MainWindow::play(){
	m_producer->set_speed(1.0);
	// If we are paused, then we need to "unlock" sdl_still.
	if(m_consumer->is_stopped()){
		m_consumer->start();
	}
	m_consumer->set("refresh", 1);
	//this->statusBar->showMessage(tr("Playing"));
	isPlaying = true;
	this->buttonLire->setText("Pause");
}

void MainWindow::pause(){
	m_producer->set_speed(0.0);
	isPlaying = false;
	this->buttonLire->setText("Lire");
}

void MainWindow::playOrPause(){
	if(isPlaying){
		//TODO il faudrait aussi changer les icons
		this->pause();
	}else{
		this->play();
	}
}

void MainWindow::seek(int position){
	int producerPosition = m_producer->get_length() / this->positionSlider->maximum() * position;
	m_producer->seek(producerPosition);
	//TODO ajouter un timer pour mettre a jour le slider au fur et a mesure que la video avance.
}

void MainWindow::arreter(){
	m_producer->set_speed(0.0);
	m_producer->seek(0);
	positionSlider->setValue(0);
	isPlaying = false;
}

void MainWindow::extraire(){
	this->extractionManagerWindow->addExtraction((int)m_producer->position());
}

void MainWindow::updateSlider(){
	if(!this->positionSlider->isSliderDown()){
		int sliderValue = 1.0 * ((int)m_producer->position()) / m_producer->get_length() * this->positionSlider->maximum();
		this->positionSlider->setValue(sliderValue);
	}
}

void MainWindow::showExtractionManagerWindow(){
	this->extractionManagerWindow->show();
	this->pause();
	m_consumer->stop();
}

